package com.btsk.dao.hibernate;

import java.util.List;

import org.hibernate.Session;

import com.btsk.dao.NewGpsInfoDao;
import com.btsk.model.NewGpsInfo;

@SuppressWarnings("unchecked")
public class NewGpsInfoDaoHibernate extends
		GenericDaoHibernate<NewGpsInfo, Long> implements NewGpsInfoDao {

	public NewGpsInfoDaoHibernate() {
		super(NewGpsInfo.class);
	}

	@Override
	public void removeByCarId(Long carId) {
		String sql = "delete from mmp_gps_info_new where car_id = :carId";
		log.debug("sql=" + sql);
		Session session = getSession(true);
		session.createSQLQuery(sql).setLong("carId", carId).executeUpdate();
		session.close();
	}

	@Override
	public List<NewGpsInfo> findAllNewGpsInfo() {
		List<NewGpsInfo> list = getHibernateTemplate().find(
				"from NewGpsInfo o where o.receiveTime is not null");
		return list;
	}

	@Override
	public NewGpsInfo findNewGpsInfoByTermno(String termno) {
		List<NewGpsInfo> list = getHibernateTemplate().find(
			"from NewGpsInfo o where o.deviceNo = ?",
			new Object[] { termno });
		
		if (list.size() > 0){
			return list.get(0);
		}else{

			return null;
		}
	}
	
	
	@Override
	public List<NewGpsInfo> findNewGpsInfoByDeviceNo(String[]deviceNo) {
		String s = "";
		for (int i=0;i<deviceNo.length;i++){
			s += "'" + deviceNo[i] + "'";
			if (i < deviceNo.length - 1){
				s += ",";
			}
		}
		return getHibernateTemplate().find("from NewGpsInfo o where o.deviceNo in (" + s + ")");
	}
	
	@Override
	public List<NewGpsInfo> findAllVehicleTypeNewGpsInfo() {
		return null;
	}
}
